<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          @click="changeBrand(item.id)"
          href="javascript:;"
          v-for="item in filterData.brands"
          :key="item.id"
          :class="{ active: item.id === filterData.brands.selectedBrand }"
          >{{ item.name }}</a
        >
      </div>
    </div>
    <!-- 下面是不同的筛选参数 -->
    <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}</div>
      <div class="body">
        <a
          @click="changeProp(item, canshu.id)"
          :class="{ active: canshu.id === item.selectedAttr }"
          href="javascript:;"
          v-for="canshu in item.properties"
          :key="canshu.id"
          >{{ canshu.name }}</a
        >
      </div>
    </div>
  </div>

  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script>
import { findSubCategoryFilter } from "@/api/category";
import { useRoute } from "vue-router";
import { ref, watch } from "vue";
export default {
  name: "SubFilter",
  setup(props, { emit }) {
    const route = useRoute();
    // 监听二级类目ID 的变化 获取筛选数据
    const filterData = ref(null);
    const filterLoading = ref(false);
    // console.log("路由", route);
    watch(
      () => route.params.id,
      (newValue) => {
        // 变化后的 ID 有值，并且 处于 二级类目路由下
        if (newValue && `/category/sub/${newValue}` === route.path) {
          // console.log("发起请求");
          filterLoading.value = true;
          findSubCategoryFilter(route.params.id).then((data) => {
            // data.result 中 brands 是品牌，saleProperties 是 这类商品包含 什么参数
            // 但是效果图 里面 每个筛选条件都有  全部  ，而 数据saleProperties、brands里面没有  全部  这个 选项 ，所以我们要加上
            data.result.brands.unshift({ id: null, name: "全部" });
            data.result.saleProperties.forEach((item) => {
              // 加一个 默认选中的 id
              item.selectedAttr = null;
              item.properties.unshift({ id: null, name: "全部" });
            });
            // 给 每一组加一个 选中的 id
            data.result.brands.selectedBrand = null;

            filterData.value = data.result;
            filterLoading.value = false;
            // console.log(data.result);
          });
        }
      },
      { immediate: true }
    );

    // 获取筛选数据的函数
    // 获取筛选参数
    const getFilterParams = () => {
      const filterParams = {};
      const attrs = [];
      filterParams.brandId = filterData.value.selectedBrand;
      filterData.value.saleProperties.forEach((p) => {
        const attr = p.properties.find((attr) => attr.id === p.selectedProp);
        if (attr && attr.id !== undefined) {
          attrs.push({ groupName: p.name, propertyName: attr.name });
        }
      });
      if (attrs.length) filterParams.attrs = attrs;
      return filterParams;
    };

    // 记录当前选择的品牌
    const changeBrand = (brandId) => {
      // console.log("当前选择品牌改变额");
      if (filterData.value.brands.selectedBrand === brandId) return;

      filterData.value.brands.selectedBrand = brandId;
      emit("filter-change", getFilterParams());
      console.log(filterData);
    };
    // 记录当前 选择的 销售属性
    const changeProp = (item, propId) => {
      if (item.selectedAttr === propId) return;
      item.selectedAttr = propId;
      emit("filter-change", getFilterParams());
    };
    return { filterData, filterLoading, changeBrand, changeProp };
  },
};
</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
.xtx-skeleton {
  padding: 10px 0;
}
</style>